package com.feilong.edgeAI.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;


import com.feilong.edgeAI.R;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtil {


    public static String getPinyin(String text){
        StringBuffer buffer = new StringBuffer(); //储存结果

        //转换函数用到的一些配置
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  //转小写
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //不带音标
        char[] chars = text.toCharArray();
        for(int i = 0; i < chars.length; ++i){
            if(chars[i] > 128){
                try{
                    String[] arr = PinyinHelper.toHanyuPinyinStringArray(chars[i],format);
                    buffer.append(arr[0]);  //转换出的结果包含了多音字，这里简单粗暴的取了第一个拼音。
                    if(i<chars.length-1){
                        buffer.append(" ");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else{ //非汉字
                buffer.append(chars[i]);
            }
        }
        return buffer.toString();
    }

    public static String getServerId(String id){
        return id.replace("+","").replace("86_","").replace("852_","").replace("853_","").replace("886_","");
    }

    public static List<String> getSubUtil(String soap){
        String rgex = "<div (.*?)</div>";
        List<String> list = new ArrayList<String>();
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        return list;
    }


    public static String getSubUtilSimple(String soap){
        String rgex = "vid=(.*?)\"";
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while(m.find()){
            return m.group(1);
        }
        return "";
    }
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    public static void saveTxt(String path,String text){
        try {
            // 创建一个FileWriter对象，不指定编码格式
            FileWriter writer = new FileWriter(path);

            // 创建一个OutputStreamWriter对象，指定编码格式为GBK
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path), "GB2312");

            // 写入数据
            out.write(text);

            // 关闭流
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static boolean containsChinese(String str) {
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }
    public static String loadJSONFromAsset(Context context, String fileName) {
        String json = null;
        try {
            AssetManager manager = context.getAssets();
            InputStream inputStream = manager.open(fileName);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
//            Log.e(TAG, "Error reading JSON file", e);
        }
        return json;
    }

    public static boolean hasChar(String text){
        return text.matches("[!@#$%^&*()_+=\\{\\}\\[\\]\"<>,./?:;'`\\|~]+");
    }
    public static int getScreenOffTime(String time){
        String[] parts = time.split(":"); // 按冒号分割小时、分钟和秒
        int minutes = Integer.parseInt(parts[0]);
        int seconds = Integer.parseInt(parts[1]);

        int milliseconds = (minutes * 60 + seconds) * 1000; // 转换为毫秒
        return milliseconds;
    }
}
